var express = require('express');
var router = express.Router();
var test01 = require('../public/javascripts/lex');
var test02 = require('../public/javascripts/first');
var test03 = require('../public/javascripts/grammar');
var test03_2 = require('../public/javascripts/grammar_ll1');
/* GET home page. */
router.get('/api/datalist',function(req,res){
    console.log("请求生体数据列表");
    res.json({
        dataList:[
            {
                data:'weight',
                name:'体重',
                unit:'kg',
                writable:true,
                from:null
            },
            {
                data:'height',
                name:'身高',
                unit:'cm',
                writable:true,
                from:null
            },
            {
                data:null,  //对应数据库里的key 这里因为没有存在数据库里所以给null
                name:'BMI指数', //用于在前端显示的名字
                unit:null,    //单位，用来显示在前端
                writable:false, //是否是可输入的数据，显示在前端
                from:['weight','/','height','*','height'] //计算式，应该由管理员创建字段时写入
            },
        ]}

    );
    res.end();
});
router.get('/api/patientlist', function (req, res) {
    console.log("请求患者列表");
    console.log(req.query);
    if (req.query.idOrName === 'zhangxiaoxiao') {
        res.json({
            patientList: [
                {
                    id: 7,
                    name: 'zhangxiaoxiao',
                    gender: "M",
                    age: 20,
                    brithdate: "2006-05-02"
                }
            ]
        })
    } else {
        res.json({
                patientList: [
                    {
                        id: 1,
                        name: 'Lily',
                        gender: "F",
                        age: 18,
                        brithdate: "2016-05-02"
                    },
                    {
                        id: 2,
                        name: 'Bob',
                        gender: "M",
                        age: 25,
                        brithdate: "2016-05-02"
                    },
                    {
                        id: 3,
                        name: 'Tony',
                        gender: "M",
                        age: 16,
                        brithdate: "2016-05-02"
                    },
                    {
                        id: 4,
                        name: 'Lucy',
                        gender: "F",
                        age: 41,
                        brithdate: "2016-05-02"
                    }, {
                        id: 5,
                        name: 'Lucy',
                        gender: "F",
                        age: 41,
                        brithdate: "2016-05-02"
                    }, {
                        id: 6,
                        name: 'Lucy',
                        gender: "F",
                        age: 41,
                        brithdate: "2016-05-02"
                    }]
            }
        )
    }
    res.end();
});

router.post('/api/login', function (req, res) {
    var user = req.body;
    console.log(user);
    if (user.accName === 'admin' && user.accPass === 'admin123456') {
        res.json({
            user: {
                userId: 777,
                acctName: 'admin',
                lastName: 'zhang',
                firstName: 'xiaoxiao',
                sex: 1,
                accountType: 1,
                createTime: '2015-12-12',
                modifyTime: '2015-12-12',
            }
        });
    } else {
        res.status(401).end();
    }

});
router.get('/favicon.ico',function(req,res){
    res.setState(404).send("go back home!");
});
router.get('/', function(req, res, next) {
    console.log('请求地址:'+req.headers.host);
    var url=req.headers.host;
    var first=url.split('.');
    switch(first[0]){
        case '14th':
        case 'www':
        case 'localhost':
        case 'bianyi':
        case '39':
            console.log('正在访问陈宗豪的小站');
            res.render('index', { title: '三月十四' });
            break;
        case 'ncair':
            console.log("正在访问软工项目页面");
            res.render('se/index');
            break;
        case 'li':
            console.log('正在访问黎东来的页面');
            res.render('li/index', { title: '银行系统' });
            break;
        default:

    }

});
// let lastreq="";
// let lasturl="";
//
// router.get('/*',function(req,res){
//     let url=req.headers.host;
//     let first=url.split('.');
//     let repath=req._parsedUrl.pathname;
//     let repath_sp=repath.split('/');
//     console.log(repath_sp[repath_sp.length-1]+'    '+lastreq);
//     if(first[0]==='ncair'&&lastreq!=repath_sp[repath_sp.length-1]){
//         lasturl=first[0];
//
//         lastreq=repath_sp[repath_sp.length-1];
//         res.redirect('/se'+repath);
//     }else{
//         lastreq=repath_sp[repath_sp.length-1];
//     }
//
// })
// router.get('/test/01',function(req,res,next){
//   res.render('test/01.html',{title:'实验01  词法分析'});
// });
// router.get('/test/02',function(req,res,next){
//     res.render('test/02.html',{title:'实验02  求first集'});
// });
router.get('/se',function(req,res){
    res.send('<script>window.onload=function(){window.location.href=\'./se/index.html\'}</script>');
});
router.get('/se/static/*',function(req,res,next){
    var repath=req._parsedUrl.pathname.split('/');
    // res.sendfile(repath);
    var filename=repath[repath.length-1];
    var rootname="/";
    for(var i=2;i<repath.length-1;i++){
        rootname+=repath[i]+'/';
    }
    res.redirect(rootname+filename)

});
router.get('/se/*.html',function(req,res){
    var repath=req._parsedUrl.pathname;
    res.render(repath.substring(1));
});
router.get('/*',function(req,res,next){
    // console.log(req._parsedUrl.pathname.substring(1));
    let repath=req._parsedUrl.path;
    // console.log(req._parsedUrl);
    let url=req.headers.host;
    let first=url.split('.');
    if(first[0]==='ncair') {
        res.redirect('/se' + repath);
        return;
    }
    switch(repath){
        default:
            res.render(repath.substring(1)+'.html',res);
    }
});
router.post('/test/01',function(req,res,next){
    console.log("test01请求：");
    console.log(req.body);
    res.json(test01.lexAll(req.body));
    res.end();
});
router.post('/test/02',function(req,res,next){
    console.log("test02请求：");
    console.log(req.body);
    res.json(test02.first(req.body));
    res.end();
});
router.post('/test/03',function(req,res,next){
    console.log("test03请求：");
    console.log(req.body);
    res.json(test03.grammar(req.body));
    res.end();
});
router.post('/test/03_2',function(req,res,next){
    console.log("test03_2请求：");
    console.log(req.body);
    res.json(test03_2.ll_1(req.body));
    res.end();
});
// router.post('/test/04',function(req,res,next){
//     console.log("test04请求：");
//     console.log(req.body);
//     res.json(test03_2.ll_1(req.body));
//     res.end();
// });










module.exports = router;

